package com.gxa.zhonglong.controller;

import com.gxa.zhonglong.common.JsonResult;
import com.gxa.zhonglong.pojo.PrGet;
import com.gxa.zhonglong.pojo.PurchaseReturn;
import com.gxa.zhonglong.pojo.Storage;
import com.gxa.zhonglong.service.PrGetService;
import com.gxa.zhonglong.service.PurchaseReturnService;
import com.gxa.zhonglong.service.StorageService;
import com.gxa.zhonglong.util.CodeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

@CrossOrigin
@Api(value = "采购退货的接口")
@RestController
@RequestMapping("/purchasereturn")
public class PurchaseReturnController {

    @Autowired
    PurchaseReturnService purchaseReturnService;
    @Autowired
    PrGetService prGetService;
    @Autowired
    StorageService storageService;
    CodeUtil codeUtil  = new CodeUtil();
    @ApiOperation("添加采购退货的方法")
    @PostMapping("/add")
    @ApiImplicitParams({
            /*@ApiImplicitParam(name="purchaseReturn",value = "添加的采购退货",required = true,paramType = "query"),*/
            @ApiImplicitParam(name= "map",value = "商品编码的集合",required = true,paramType = "body")
    })
    public Map add(/*@RequestParam*/ PurchaseReturn purchaseReturn, @RequestBody  Map<String,List> map){
        List<String> numberList = map.get("ids");
        System.out.println(purchaseReturn);
        purchaseReturn.setPurchaseReturnNum(codeUtil.CGTHDencode());
        purchaseReturn.setPurchaseReturnCtime(new Timestamp(System.currentTimeMillis()));
        purchaseReturnService.add(purchaseReturn);
        System.out.println(numberList);
        for(int i=0;i<numberList.size();i++){
            PrGet prGet = new PrGet();
            prGet.setPrGetId(purchaseReturn.getPurchaseReturnNum());
            prGet.setPrGetGid(numberList.get(i));
            prGet.setPrGetAmount(storageService.selectByCount(numberList.get(i)));
            prGet.setPrGetWeight(storageService.selectByWeight(numberList.get(i)));
            prGet.setPrGetCost(storageService.selectByCost(numberList.get(i)));
            prGetService.add(prGet);
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("添加成功");
        return jsonResult.getDataMap();
    }


    @PostMapping("/getListByID")
    @ApiOperation("通过多个商品编码来批量获取商品信息的方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "map",value = "商品编码的集合",required = true,paramType  = "body")
    })
    public Map getListByID(@RequestBody  Map<String,List> map ){
        List list = (List)map.get("ids");
        System.out.println(list);
        List<Storage> listByID = purchaseReturnService.getListByID(list);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(listByID);
        return jsonResult.getDataMap();
    }

    @PostMapping("/selectByNum")
    @ApiOperation("通过单据编号获得采购退货单信息的方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "number",value = "单据编号",paramType = "String")
    })
    public  Map selectByNum(String number){
        PurchaseReturn purchaseReturn = purchaseReturnService.selectByNum(number);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(purchaseReturn);
        return  jsonResult.getDataMap();
    }
    @GetMapping("/reject")
    @ApiOperation("审批不通过的方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "number",value = "单据编号",paramType = "String"),
            @ApiImplicitParam(name="ename",value = "审批人名字",paramType = "String"),
            @ApiImplicitParam(name="etime",value = "审批时间",paramType = "Timestamp")
    })
    public  Map reject(String ename,Timestamp etime,String number){
        purchaseReturnService.reject(ename,etime,number);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("审批成功");
        return jsonResult.getDataMap();
    }

    @GetMapping("/approve")
    @ApiOperation("审批通过的方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "number",value = "单据编号",paramType  = "String"),
            @ApiImplicitParam(name="ename",value = "审批人名字",paramType = "String"),
            @ApiImplicitParam(name="etime",value = "审批时间",paramType = "Timestamp")
    })
    public  Map approve(String ename, Timestamp etime,String number){
        List<String> list = prGetService.selectByNum(number);
        for(int i=0;i<list.size();i++){

            storageService.delete(list.get(i));
        }
        purchaseReturnService.approve(ename,etime,number);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("审批成功");
        return jsonResult.getDataMap();
    }
    @PostMapping("/delete")
    @ApiOperation("删除单个订单中的商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name="num",value = "商品编码",paramType = "String")
    })
    public Map delet(String num){
        prGetService.delete(num);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("删除成功");
        return  jsonResult.getDataMap();

    }


}
